package com.syl.servlet;

import com.google.gson.Gson;
import com.syl.service.UserService;
import com.syl.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname UserDelectServlet
 * @Description
 * @Date 2025/8/2 14:29
 * @Created by SYL
 */

@WebServlet("/user/delect")
public class UserDelectServlet extends HttpServlet {

    UserService userService =  new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");

         boolean ok =  userService.delectUser(Integer.parseInt(id));


        // 构造符合前端期望的响应格式
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg",ok );
        System.out.println("result = " + result);

        // 使用 Gson 将对象转换为 JSON 字符串
        Gson gson = new Gson();
        String json = gson.toJson(result);
        System.out.println("json = " + json);

        // 写入响应
        resp.getWriter().write(json);

    }
}
